<div class="friend-content-gh">
	<div style="margin:0 13px 13px 13px;">
		<i:literal-nav xmlns:i="178">
		   <i:literal-nav-item label="好友的好友" url="<?= Controller::_makeURL('friends', 'group', 'mutu_friends')?>" <?if ($this->action=='mutu_friends'):?>selected="true"<?endif;?>/>
		   <i:literal-nav-item label="公会好友" url="<?= Controller::_makeURL('friends', 'guild', 'index')?>" <?if ($this->controller=='guild'):?>selected="true"<?endif;?>/>
		   <i:literal-nav-item label="您可能认识的人" url="<?= Controller::_makeURL('friends', 'armory', 'index')?>" <?if ($this->controller=='armory'):?>selected="true"<?endif;?>/>
	 	</i:literal-nav>
	</div>
<?if (count($this->tem['ROWS'])):?>
	<div class="seplinetop">
		<form name="search_form" method="post" action="/~friends.guild.prepare" onsubmit='return Friends.formRequest(this);' >
		<div class="gh-friend-search" style="margin-top:-10px">
			<div class="txt">请输入同公会会员名称：</div>
			<div class="fixie6png search-focus">
				<input type="text" class="search-input" name="add_uid"/>
				<button class="fixie6png search-btn" type="button" onclick="search_form.onsubmit();"></button>
			</div>
		</div><!--gh-friend-search -->
		</form>
		共有<strong class="red" id="guser_count"><?php echo $this->tem['INFO']['COUNT'];?></strong>位未邀请的公会成员
	</div><!-- seplinetop-->
	
	<div id="users_div">
    	<img id="loagimg" src="/images/loading.gif" style='display:none'>
    	<div id="page1">
    	<div class="gh-friend-list">
			
			<?foreach($this->tem['ROWS'] as $i=>$row):?>
			<div class="gh-friend" <?++$i;if ($i % 10 == 0):?>style="margin-right:0"<?endif;?> id="li_<?=$row['id']?>">
				<div class="gh-friend-main">
					<div class="avatar50"><a href="/?uid=<?=$row['id']?>" target="_blank"><img src="<?=avatar_util_saver::get_path($row['id'], 50)?>" title="点击进入个人主页"/></a></div>
					<div class="leftfix">
						<label for="<?=$row['id']?>" title="<?=htmlspecialchars($row['username'])?>">
							<input type="checkbox" name="guild_user" id="<?=$row['id']?>" value="<?=htmlspecialchars(mb_strimwidth($row['username'] , 0 , 8 , '', 'utf-8'))?>" title="<?=htmlspecialchars($row['username'])?>" onclick="javascript:countCheck(this);" /><?=htmlspecialchars(mb_strimwidth($row['username'] , 0 , 8 , '', 'utf-8'))?>
						</label>
					</div>
				</div>
			</div>
			<?endforeach; ?>
				<div class="clearfix"></div>
				<div class="gh-friend-btn">
					<button class="cbtn" onclick="selectAll('<?=$this->page?>')">全选</button>
					<button class="cbtn" onclick="noSelect('<?=$this->page?>')">反选</button>
				</div>
			</div><!--gh-friend-list -->
			<div class="sepline13"><?=$this->pageInfo?></div>
			
			</div>
    </div>
	
    <div class="gh-friend-selected-count" id="select_div">
		<div class="count-title">单次添加人数不能超过30人： <span><strong id="select_user_nums">0</strong> / 30</span></div>
		<div id="selected"></div>
	</div>
	<div class="count-btn"><button class="cbtn1" id="invite" onclick="javascript:send_invite();">发送邀请</button></div>
	<!--gh-friend-selected-count -->
<?else:?>
	<i:info type="friendly" xmlns:i="178">你还没有加入任何公会, <a href='http://gh.178.com' target='_blank'>进入公会</a></i:info>
<?endif;?>
</div> <!--end friend-content-gh -->


<script language="javascript">
//最大选中数
var maxGuildUsers = 30;

//全选
function selectAll(page_num)
{
	//获取选中的会员
	var selectUsers = $$('input[type="checkbox"][name="guild_user"]').select(function(i){return i.checked});
	var len = selectUsers.length;

	var page_id = 'page' + page_num;
	var count = 0;
	$(page_id).select('input[type="checkbox"][name="guild_user"]').each(function(i) {
		if (!i.checked) {//没有被选中
			count++;
			
			//会员检测
			if ((len + count) > maxGuildUsers) {
				alert("最多只能选择 " + maxGuildUsers + " 位会员！");
				throw $break; 
			}
			
			i.checked = true;
			
			//加底框
			var li_id = 'li_' + i.id;
			$(li_id).className = 'gh-friend  gh-friend-selected';
		}
	});
	
	showUsers();
}

//全不选
function noSelect(page_num)
{
	var page_id = 'page' + page_num;
	$(page_id).select('input[type="checkbox"][name="guild_user"]').each(function(i) {
		i.checked = false;
		
		var li_id = 'li_' + i.id;
		$(li_id).className = 'gh-friend';
	});
	
	showUsers();
}

//选中单个会员检测
function countCheck(obj)
{
	//获取选中的会员
	var selectUsers = $$('input[type="checkbox"][name="guild_user"]').select(function(i){return i.checked});
	var len = selectUsers.length;
	var li_id = 'li_' + obj.id;
	
	if (obj.checked) {
		if (len > maxGuildUsers) {
			obj.checked = false;
			alert("最多只能选择 " + maxGuildUsers + " 位会员！");
			return false;
		} else {
			$(li_id).className = 'gh-friend  gh-friend-selected';
		}
	} else {
		$(li_id).className = 'gh-friend';
	}
	
	showUsers();
}

//删除选中会员
function deleteUser(id)
{
	$(id).checked = false;
	
	var li_id = 'li_' + id;
	$(li_id).className = 'gh-friend';
	
	showUsers();
}

//显示被选中的会员
function showUsers()
{
	var html = '';
	
	//全部选中的会员
	var selected_user = $$('input[type="checkbox"][name="guild_user"]').select(function(i){return i.checked});
	var len = selected_user.length;
	for (var i = 0; i < len; i++) {
		if (i % 8 == 0) {
			html += '<ul>';	
		}
		//html += '<li title="'+selected_user[i].title+'"><input name="" type="button"  class="omiticon" onclick="javascript:deleteUser(' + "'" + selected_user[i].id + "'" + ');"/>' + selected_user[i].value + '</li>';
		html += '<li title="'+selected_user[i].title+'"><Span title="删除" onclick="javascript:deleteUser(' + "'" + selected_user[i].id + "'" + ');">×</Span>' + selected_user[i].value + '</li>'
		if (((i + 1) % 8 == 0) || (i + 1 == len)) {
			html += "</ul>";	
		}
	}
	
	$('select_user_nums').update(len);
	$('selected').update(html);
}

//分页显示
function show_page(page_num)
{
	var page_id = 'page' + page_num;
	
	//存在相应分页数据
	if ($(page_id)) {
		$$('#users_div > div').each(function(i){$(i).hide();});
		$(page_id).show();
        return;
	}
 	
	$$('#users_div > div').each(function(i){$(i).hide();});	
	$('loagimg').show();
	new Ajax.Request("/~friends.guild.user_list",{
		method: 'post',
		parameters: 'page=' + page_num,
		onComplete: function(transport){
			var ret = transport.responseText;
			if (ret) {
				$('loagimg').hide();
				var page_div = new Element('div', { id: page_id });//创建分页div
				page_div.update(ret);
				$('users_div').insert(page_div);//插入分页数据到user_div
			} else {
				new MessageBox(MessageBox.TYPE_MESSAGE, '获取数据失败,请刷新页面重试', '提示', {close:function(){}});
			}
		}
	});
}

//发送邀请
function send_invite()
{
	var guild_users = '';
	$$('input[type="checkbox"][name="guild_user"]').select(function(i){return i.checked}).each(function(i) {
	 	guild_users += (guild_users == '') ? '' : ',';
	    guild_users += i.id;
	});

	if ('' == guild_users) {
		new MessageBox(MessageBox.TYPE_MESSAGE, '请选择要发送邀请的会员', '提示', {close:function(){}});
		return;
	}
	
	$('invite').disabled = true;
	new Ajax.Request("/~friends.guild.invite",{
		method: 'post',
		parameters: 'uids=' + guild_users,
		onComplete: function(transport){
			var ret = (transport.responseText).evalJSON();
			if (ret.result) {
				window.location.href= '/~friends.guild.index';
				document.documentElement.scrollTop = 0;
			} else {
				//new MessageBox(MessageBox.TYPE_MESSAGE, '发送邀请失败,请重试', '提示', {close:function(){}});
				MessageBox.showError(ret);
			}
		}
	});
}

///Event.observe(window, 'load', function() {   
//    show_page(1);
//});
</script>